<template>
  <div class="star-coin">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="item-msg" v-if="$store.state.userInfo.group === 'administrator'">
        <label>钱包</label>
        <el-select v-model="searchForm.platform_id" placeholder="全部" size="mini" @change="init()">
          <el-option v-for="item in wallets" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>
      <div class="item-msg">
        <label>类型</label>
        <el-select v-model="wallet_type" placeholder="全部" size="mini" @change="init()">
          <el-option :value="1" label="去中心化钱包"></el-option>
          <el-option :value="2" label="托管钱包"></el-option>
        </el-select>
      </div>
      <div class="item-msg">
        <label>推荐类别</label>
        <el-select v-model="searchForm.category_id" placeholder="全部" size="mini" @change="search()">
          <el-option
            v-for="(item,index) in typeList"
            :key="index"
            :value="item.id"
            :label="item.name['zh-CN']"
          ></el-option>
        </el-select>
      </div>
      <!-- <el-button class="search-btn" @click="search()" size="mini" type="primary">查询</el-button> -->
      <el-button class="add-btn" size="mini" type="primary" @click="showDialog=true">+ 添加币种</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="small"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="coin.name" label="币种名称" width="120"></el-table-column>
        <el-table-column align="center" prop="icon" label="图标">
          <template slot-scope="item">
            <img :src="item.row.coin.icon" class="scaleImg" @click="viewImg(item.row.coin.icon)">
          </template>
        </el-table-column>
        <el-table-column align="center" prop="sort" label="排序">
          <template slot-scope="item">
            <div>
              {{item.row.sort}}
              <el-popover
                placement="right"
                trigger="click"
                v-model="item.row.showPopover"
                @after-leave="editForm.sort='';editForm.id=''"
              >
                <el-input
                  size="mini"
                  width="100"
                  style="width:120px;margin-right:5px"
                  placeholder="排序"
                  v-model.number="editForm.sort"
                ></el-input>
                <el-button
                  size="mini"
                  type="primary"
                  @click="edit(item.row.id);item.row.showPopover=false"
                >确定</el-button>
                <i class="iconfont iconweb-icon-" slot="reference"></i>
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="平台" prop="coin.platform"></el-table-column>
        <!-- <el-table-column align="center" prop="card_type" label="推荐类型" width="170">
          <template slot-scope="item">{{item.row.recommend===1?'首页推荐':'次页推荐'}}</template>
        </el-table-column>-->
        <!-- <el-table-column align="center" prop="CoinInfo.chain" label="所属链"></el-table-column> -->
        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="item">
            <el-button type="text" @click="delToken(item.row)" class="del-btn">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
        :pages="searchForm"
        :total="total"
        @pageSizeChange="pageSizeChange"
        @pageChange="pageChange"
      ></pages>
      <!-- 查看图片弹窗 -->
      <transition name="fade">
        <img-view :img="imgUrl" v-if="showImg" @cancel="showImg=false"></img-view>
      </transition>
      <!-- 添加币种弹窗 -->
      <transition name="fade">
        <add-star
          v-if="showDialog"
          @cancel="showDialog=false"
          @success="showDialog=false;search()"
          :platform_id="searchForm.platform_id"
        ></add-star>
      </transition>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import ImgView from "@/components/Explorer/ImgView.vue";
import AddStar from "@/components/CoinsManage/AddStarCoin.vue";
import {
  getComTokenList,
  updateComToken,
  getStarCategory,
  delComToken
} from "./Coins.service.js";
export default {
  components: {
    Pages,
    ImgView,
    AddStar
  },
  computed: {
    wallets() {
      return this.$store.state.wallet_list || [];
    }
  },
  data() {
    return {
      loading: false,
      wallet_type: 1,
      searchForm: {
        platform_id: String(this.$store.state.userInfo.platform_id),
        page: 1,
        size: 10,
        category_id: ""
      },
      editForm: {
        id: "",
        sort: ""
      },
      typeList: [],
      tableData: [],
      total: 10,
      showDialog: false,
      showImg: false,
      imgUrl: ""
    };
  },

  mounted() {
    this.init();
  },
  methods: {
    async init() {
      let param = {
        platform_id: Number(this.searchForm.platform_id),
        status: 1,
        type: Number(this.wallet_type)
      };
      const res = await getStarCategory(param);
      this.typeList = res.data.items || [];
      if (this.typeList.length !== 0)
        this.searchForm.category_id = this.typeList[0].id;
      else this.searchForm.category_id = "";
      this.search();
    },
    async search() {
      this.loading = true;
      let param = JSON.parse(JSON.stringify(this.searchForm));
      if (this.searchForm.category_id === "") param.category_id = 99999;
      const res = await getComTokenList(param);
      this.tableData = res.data.items || [];
      this.total = res.data.total;
      this.loading = false;
    },
    async delToken(item) {
      this.$confirm(
        `是否删除 <span class="confirm-tip-color">${
          item.coin.name
        }</span> 币种?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true
        }
      )
        .then(async () => {
          const res = await delComToken({
            id: item.id
          });
          if (res.code === 0) {
            this.$message.success("删除成功!");
            this.search();
          }
        })
        .catch(() => this.$message.info("已取消删除"));
    },
    async edit(id) {
      this.editForm.id = id;
      const res = await updateComToken(this.editForm);
      if (res.code === 0) {
        this.$notify.success({
          title: "提示",
          message: "修改成功",
          duration: 2000
        });
        this.search();
      }
    },
    viewImg(url) {
      this.imgUrl = url;
      this.showImg = true;
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search();
    },
    pageSizeChange(val) {
      this.searchForm.size = val;
      this.searchForm.page = 1;
      this.search();
    }
  }
};
</script>

<style lang="scss">
.star-coin {
  // position: relative;
  min-height: calc(100vh - 100px);
  .search-block {
    width: 100%;
    padding: 40px 30px;
    border-radius: 5px;
    background: #fff;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .add-btn {
      margin-left: 15px;
    }
    .item-msg {
      display: inline-block;
      margin-right: 20px;
      font-size: 1 size= "medium" 4;
      color: $admin_font_param;
      label {
        display: inline-block;
        max-width: 90px;
        text-align: right;
        margin-right: 10px;
      }
      .el-input {
        width: 150px;
      }
      .el-select {
        width: 150px;
      }
    }
    .search-btn {
      margin-left: 25px;
    }
  }
  .data-table {
    // position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 30px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    .del-btn {
      color: #f56c6c;
    }
    .iconweb-icon- {
      color: #409eff;
      cursor: pointer;
    }
    .scaleImg {
      max-width: 120px;
      max-height: 30px;
      cursor: pointer;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
}
</style>